﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BloomsburyBanHang.DAO;

namespace BloomsburyBanHang
{
    public partial class shoppingcart1 : System.Web.UI.Page

    {
        public double totalPrice = 0.0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string idProduct = Request["idProduct"];
            string type = Request["type"];
            if (type == "add")
            {
                Session["shoppingcart"] += ',' + idProduct;
                string temp = Session["shoppingcart"] as string;
                string[] shoppingCartItem = temp.Split(',');
              //  string[] shoppingCartItem = shoppingcart.Split(',');
                for (int i = 1; i < shoppingCartItem.Count(); i++)
                {
                    SAN_PHAM sp = new SAN_PHAM();
                    sp = SanPhamDAO.TimSanPhamTheoMa(Convert.ToInt32(shoppingCartItem[i]));
                  
                    totalPrice += (double)sp.Gia;
                }
                Session["total"] = totalPrice.ToString();
            }

            else
            {
                string temp = Session["shoppingcart"] as string;
                string[] shoppingCartItem = temp.Split(',');
                List<string> ListCartItem = new List<string>();
                for (int i = 1; i < shoppingCartItem.Count(); i++)
                {
                    ListCartItem.Add(shoppingCartItem[i]);
                }
                for (int i = 0; i < ListCartItem.Count; i++)
                {
                    if (ListCartItem[i].Equals(idProduct))
                    {
                        ListCartItem.RemoveAt(i);
                        break;
                    }
                }
                temp = "";
                for (int i = 0; i < ListCartItem.Count; i++)
                {
                    temp += ',' + ListCartItem[i];
                }

                Session["shoppingcart"] = temp;
                //string temp = Session["shoppingcart"] as string;
                shoppingCartItem = temp.Split(',');
                //  string[] shoppingCartItem = shoppingcart.Split(',');
                for (int i = 1; i < shoppingCartItem.Count(); i++)
                {
                    SAN_PHAM sp = new SAN_PHAM();
                    sp = SanPhamDAO.TimSanPhamTheoMa(Convert.ToInt32(shoppingCartItem[i]));

                    totalPrice += (double)sp.Gia;
                }
                Session["total"] = totalPrice.ToString();
            }

            Response.Write("ok");
            Response.End();
            return;
        }
    }
}